//
//  ZH_MallViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/13.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_MallViewController.h"
#import "ZH_MallHomeImageTableViewCell.h"
#import "ZH_MallHomeTypeTableViewCell.h"
#import "ZH_MallHotTableViewCell.h"
#import "ZH_HotRecommendedableViewCell.h"
#import "ZH_MallCategoryViewController.h"
#import "ZH_MallOrderViewModel.h"

@interface ZH_MallViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)ZH_MallOrderViewModel *viewModel;
@end

@implementation ZH_MallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    [self bannerData];
    //首页分类的
    [self cateArrayData];
    //热销商品的获取
    [self goodsHotData];
    //推荐商品获取
    [self goodsGroomData];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)loadNavigationBackButton{
    
}
#pragma mark - DATA
- (void)bannerData{
    WEAKSELF;
    [self.viewModel goodsBannerResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }

    }];
}
 //首页分类的button
- (void)cateArrayData{
    WEAKSELF;
    [self.viewModel goodsCateResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
//热销商品的获取
- (void)goodsHotData{
    WEAKSELF;
    [self.viewModel hotGoodsResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
//推荐商品获取
- (void)goodsGroomData{
    WEAKSELF;
    [self.viewModel groomGoodsResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
- (void)initUserInterface{
    self.navigationItem.title = @"智慧商城";
    self.view.backgroundColor = kCommonBackgroudColor;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_yjfk"] style:UIBarButtonItemStylePlain target:self action:nil];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
}
#pragma mark - getter,setter
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 1){
        return FITSCALE(9);
    }
    else if(section == 2){
        return FITSCALE(9);
    }
    else if(section == 3){
        return FITSCALE(9);
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ZH_MallHomeImageTableViewCell *cell = [ZH_MallHomeImageTableViewCell cellWithTableView:tableView];
        [cell setPicHeight:FITSCALE(200)];
        [cell updateUIForImages:self.viewModel.goodsBannerArray andDefaultImage:@"img_banner"];
        return cell;
    }
    else if (indexPath.section == 1){
        ZH_MallHomeTypeTableViewCell *cell = [ZH_MallHomeTypeTableViewCell cellWithTableView:tableView];
        cell.backgroundColor = kCommonBackgroudColor;
        [cell updateUiForData:self.viewModel.goodsCateArray];
        WEAKSelf;
        cell.onButtonClick = ^(NSInteger index) {
            ZH_MallCategoryViewController *vc = [[ZH_MallCategoryViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        return cell;
    }
    else if (indexPath.section == 2){
        ZH_MallHotTableViewCell *cell = [ZH_MallHotTableViewCell cellWithTableView:tableView];
        cell.backgroundColor = kCommonBackgroudColor;
        [cell updateUiForData:self.viewModel.hotGoodsArray];
        return cell;
    }
    else if(indexPath.section == 3){
        ZH_HotRecommendedableViewCell *cell = [ZH_HotRecommendedableViewCell cellWithTableView:tableView];
        cell.backgroundColor = kCommonBackgroudColor;
        [cell updateUiForData:self.viewModel.groomGoodsArray];
        return cell;
    }
    return nil;
}
#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (ZH_MallOrderViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_MallOrderViewModel alloc] init];
    }
    return _viewModel;
}
@end
